package icedot.library.common.ui;

import icedot.library.common.R;
import icedot.library.common.utils.CommonFun;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class UpdateHead extends LinearLayout
{
	public enum Status
	{
		RESET,		  //复位
		PRESSEDDOWN,  //按下
		PRESSEDMOVE,  //按下移动
		PRESSEDUP,	  //松开
		UPDATEDATA,   //等待数据
	}
	private String[] _hintList = new String[3]; //每个动作的提示
	
	private LinearLayout 		_layoutMain;
	private ImageView 			_imageLoading;
	private TextView 			_textTime;
	
	private int 				_minUpdateHead;   	//拉动更新的最小尺寸
	private int 				_maxUpdateHead;     //最多能拉多高
	private Animation 			_clockWise;  		//顺时针动画
	private Status 				_state = Status.RESET;				//头部的状态
	private UpdateViewListener _notify;
	private FootListener _parentView;
	
	public UpdateHead(Context context)
	{
		super(context);
		initView(context);
	}

	public UpdateHead(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initView(context);
	}

	/**
	 * 默认头不显示任何内容
	 * @param context
	 */
	private void initView(Context context)
	{
		LayoutParams mainParams = new LayoutParams(
				LayoutParams.MATCH_PARENT, 0);
		_layoutMain = (LinearLayout) LayoutInflater.from(context).inflate(
				R.layout.update_head, null);
		addView(_layoutMain, mainParams);
		
		_imageLoading = (ImageView)findViewById(R.id.exlistviewhead_image_loading);
		_textTime = (TextView)findViewById(R.id.exlistviewhead_text_time);
		_textTime.setVisibility(View.GONE);
		_imageLoading.setVisibility(View.GONE);
		
		_minUpdateHead = CommonFun.dip2px(context,60);
		_maxUpdateHead = CommonFun.dip2px(context,120);

		_hintList[0] = "继续下拉...";
		_hintList[1] = "放开更新...";
		_hintList[2] = "正在更新...";
	}
	
	public void setContent(String content)
	{
		if(_textTime != null)
		{
			_textTime.setVisibility(View.VISIBLE);
			_textTime.setText(content);
		}
	}

	public void setNotifyListener(UpdateViewListener notify,FootListener parentView)
	{
		_notify = notify;
		_parentView = parentView;
	}

	public void setContent(Status status, String content)
	{
		if(status == Status.PRESSEDDOWN || status == Status.PRESSEDMOVE)
		{
			_hintList[0] = content;
		}
		else if(status == Status.PRESSEDUP)
		{
			_hintList[1] = content;
		}
		else if(status == Status.UPDATEDATA)
		{
			_hintList[2] = content;
		}
	}
	
	/**
	 * 设置动画.和显示内容
	 */
	public void useHeadContent()
	{
		_textTime.setVisibility(View.VISIBLE);
		_imageLoading.setVisibility(View.VISIBLE);

		_clockWise = AnimationUtils.loadAnimation(this.getContext(),R.anim.exlistview_head_image_rote);
	}

	/**
	 * 设置更新时的高度
	 * @param height
     */
	public void setUpdateHeight(int height)
	{
		_minUpdateHead = height;
	}
	
	public void setVisiableHeight(int height)
	{
		if(height < 0)
		{
			height = 0;
		}
		
		LayoutParams mainParams =
				(LayoutParams)_layoutMain.getLayoutParams();
		mainParams.height = height;
		_layoutMain.setLayoutParams(mainParams);

		if(height >= _minUpdateHead)
		{
			_imageLoading.setVisibility(View.VISIBLE);
			if(_imageLoading.getAnimation() == null)
			{
				_imageLoading.setAnimation(_clockWise);
				_imageLoading.startAnimation(_clockWise);
			}
		}
		else
		{
			stopAnimation();
		}
	}
	
	public void setState(Status state)
	{
		if(state == Status.UPDATEDATA)
		{
			return;
		}

		if(state == Status.RESET && _state == Status.UPDATEDATA)
		{
			_state = state;
		}
		else if(_state != Status.UPDATEDATA && state != Status.RESET)
		{
			_state = state;
			if(state == Status.PRESSEDUP)
			{
				if(!needUpdate())
				{
					_state = Status.RESET;
				}
			}
			else if(state == Status.PRESSEDDOWN)
			{
				setContent(_hintList[0]);
			}
			else if(state == Status.PRESSEDMOVE)
			{
				if(getVisiableHeight() >= getMinUpdateHeight())
				{
					setContent(_hintList[1]);
				}
			}
		}
	}

	public Status getState()
	{
		return _state;
	}
	
	public int getVisiableHeight()
	{
		return _layoutMain.getHeight();
	}
	public int getMinUpdateHeight()
	{
		return _minUpdateHead;
	}
	public int getMaxUpdateHead()
	{
		return _maxUpdateHead;
	}
	
	public boolean needUpdate()
	{
		if(_notify == null)
		{
			return false;
		}
		if(_state == Status.UPDATEDATA)
		{
			return true;
		}

		LayoutParams mainParams =
				(LayoutParams)_layoutMain.getLayoutParams();
		if(mainParams.height >= _minUpdateHead)
		{
			_state = Status.UPDATEDATA;
			_notify.onHeadRefresh();
			_parentView.clearFootFinish();
			setContent(_hintList[2]);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public void stopAnimation()
	{
		_imageLoading.clearAnimation();
	}

	public interface FootListener
	{
		void clearFootFinish();		//清除底部的加载完成
	}
}
